﻿//--------------------------------------------------------------------------------------------------------------
// <copyright file="RegistryAccess.cs" company="Microsoft Corporation" author="David Martos" date="01/02/2008">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//--------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;


namespace CSP.Package
{
    /// <summary>
    /// 
    /// </summary>
    public class RegistryAccess
    {
        private static RegistryAccess instance;


        /// <summary>
        /// Class constructor. Only called from GetInstance.
        /// </summary>
        private RegistryAccess()
        {
        }

        /// <summary>
        /// Get a singleton reference to RegistryAccess class
        /// </summary>
        /// <returns>RegistryAccess object</returns>
        public static RegistryAccess GetInstance()
        {
            if (instance == null)
            {
                instance = new RegistryAccess();
            }
            return instance;
        }

        /// <summary>
        /// Get a property from the system registry, provided a registry group, a key name
        /// and a property name
        /// </summary>
        /// <param name="registryGroup">Registry group</param>
        /// <param name="keyName">Registry key name</param>
        /// <param name="propertyName">Property name</param>
        /// <returns>Property value</returns>
        public static string GetStringProperty(Microsoft.Win32.RegistryKey registryGroup,
            string keyName, string propertyName)
        {
            string result = string.Empty;
            Microsoft.Win32.RegistryKey regKey = registryGroup;
            regKey = regKey.OpenSubKey(keyName);
            result = regKey.GetValue(propertyName).ToString();
            regKey.Close();
            return result;
        }


        /// <summary>
        /// Get a property from the system registry, provided a registry group, a key name
        /// and a property name
        /// </summary>
        /// <param name="registryGroup">Registry group</param>
        /// <param name="keyName">Registry key name</param>
        /// <param name="propertyName">Property name</param>
        /// <returns>Property value</returns>
        public static byte[] GetByteArrayProperty(Microsoft.Win32.RegistryKey registryGroup,
            string keyName, string propertyName)
        {
            byte[] result;
            Microsoft.Win32.RegistryKey regKey = registryGroup;
            regKey = regKey.OpenSubKey(keyName);
            result = (byte[])regKey.GetValue(propertyName);
            regKey.Close();
            return result;
        }

        /// <summary>
        /// Get if a property exists into the system registry, provided a registry group, a key name
        /// and a property name
        /// </summary>
        /// <param name="registryGroup">Registry group</param>
        /// <param name="keyName">Registry key name</param>
        /// <param name="propertyName">Property name</param>
        /// <returns>boolean</returns>
        public static bool PropertyExists(Microsoft.Win32.RegistryKey registryGroup,
            string keyName, string propertyName)
        {
            try
            {
                string result = string.Empty;
                Microsoft.Win32.RegistryKey regKey = registryGroup;
                regKey = regKey.OpenSubKey(keyName);
                result = regKey.GetValue(propertyName).ToString();
                regKey.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Get a property from the system registry, provided a registry group, a key name
        /// and a property name
        /// </summary>
        /// <param name="registryGroup">Registry group</param>
        /// <param name="keyName">Registry key name</param>
        /// <param name="propertyName">Property name</param>
        /// <returns>Property value</returns>
        public static void SetStringProperty(Microsoft.Win32.RegistryKey registryGroup,
            string keyName, string propertyName, string value)
        {
            try
            {
                string result = string.Empty;
                Microsoft.Win32.RegistryKey regKey = registryGroup;
                regKey = regKey.OpenSubKey(keyName, true);
                regKey.SetValue(propertyName, value);
                regKey.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Create a string property in the system registry, provided a registry group, a key name
        /// a property name and a value
        /// </summary>
        /// <param name="registryGroup">Registry group</param>
        /// <param name="keyName">Registry key name</param>
        /// <param name="propertyName">Property name</param>
        /// <returns>Property value</returns>
        public static void CreateStringProperty(Microsoft.Win32.RegistryKey registryGroup,
            string keyName, string propertyName, string value)
        {
            string result = string.Empty;
            Microsoft.Win32.RegistryKey regKey = registryGroup;
            regKey = regKey.CreateSubKey(keyName);
            regKey.SetValue(propertyName, value);
            regKey.Close();
        }


        /// <summary>
        /// Create a string property in the system registry, provided a registry group, a key name
        /// a property name and a value
        /// </summary>
        /// <param name="registryGroup">Registry group</param>
        /// <param name="keyName">Registry key name</param>
        /// <param name="propertyName">Property name</param>
        /// <returns>Property value</returns>
        public static void CreateByteArrayProperty(Microsoft.Win32.RegistryKey registryGroup,
            string keyName, string propertyName, byte[] value)
        {
            string result = string.Empty;
            Microsoft.Win32.RegistryKey regKey = registryGroup;
            regKey = regKey.CreateSubKey(keyName);
            regKey.SetValue(propertyName, value, Microsoft.Win32.RegistryValueKind.Binary);
            regKey.Close();
        }



        /// <summary>
        /// Delete property from the system registry, provided a registry group, a key name
        /// and a property name
        /// </summary>
        /// <param name="registryGroup">Registry group</param>
        /// <param name="keyName">Registry key name</param>
        /// <param name="propertyName">Property name</param>
        /// <returns>Property value</returns>
        public static void DeleteStringProperty(Microsoft.Win32.RegistryKey registryGroup,
            string keyName, string propertyName)
        {
            try
            {
                string result = string.Empty;
                Microsoft.Win32.RegistryKey regKey = registryGroup;
                regKey = regKey.CreateSubKey(keyName);
                regKey.DeleteValue(propertyName);
                regKey.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Return the network credentials for Contoso that are currently configured in 
        /// system registry
        /// </summary>
        /// <param name="usernameKey">Key name for credentials username</param>
        /// <param name="passwordKey">Key name for credentials password</param>
        /// <param name="domainKey">Key name for credentials domain</param>
        /// <returns>NetworkCredential object</returns>
        public static System.Net.NetworkCredential GetNetworkCredentials(string usernameKey,
            string passwordKey, string domainKey)
        {
            System.Net.NetworkCredential result = null;
            try
            {
                Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine;
                regKey = regKey.OpenSubKey(Constants.CspRegistryKey);
                string username = regKey.GetValue(usernameKey).ToString();
                string password = regKey.GetValue(passwordKey).ToString();
                string domain = regKey.GetValue(domainKey).ToString();

                result = new System.Net.NetworkCredential(username, password, domain);
                regKey.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
